#include "sckwd.h"
//
sckwd::sckwd(QWidget *pa)  : QDialog(pa)
{
    model = new QSqlQueryModel;
    view = new QTableView(this);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->horizontalHeader()->hide();
    view->verticalHeader()->hide();
    this->setWindowFlags(this->windowFlags() |= Qt::FramelessWindowHint);
    this->hide();
    parent=pa;
    this->setFont(parent->font());
}
//
void sckwd::keyPressEvent(QKeyEvent* key)
{
    if(key->key()==Qt::Key_Escape){
        this->hide();
        rt.clear();
    }
    else if(key->key()==Qt::Key_Return || key->key()==Qt::Key_Enter){
        this->hide();
        int fp=0;		//field position
        rt.clear();
        while(fp<fieldsCount){
            rt << model->record(view->currentIndex().row()).value(fp).toString();
            fp++;
        }
    }
}
//
//	QStringList sckwd::getValue(QSqlQuery * s, int showCount); //show in center 
//	QStringList sckwd::getValue(QSqlQuery * s, int showCount , int x , int y,int linesCount);
//
QStringList sckwd::getValue(QSqlQuery * s, int showCount , int x , int y , int w, int linesCount)
{
    if(s->size()<1){
        QMessageBox::warning(this,tr("注意"),tr("找不到指定的检索内容！"));
        rt.clear();
        return rt;
    }
    if(s->size()==1){
        s->first();
        int fp=0;		//field position
        fieldsCount=s->record().count();
        rt.clear();
        while(fp<fieldsCount){
            rt << s->value(fp).toString();
            fp++;
        }
        return rt;
    }
    model->setQuery(*s);
    view->setModel(model);
    int fontSize=view->font().pointSize();
    int fp=0;		//field position
    while(fp<showCount)	{//set shown fields width
        int sz;
        if(s->record().field(fp).type() ==QVariant::String){
            sz=s->record().field(fp).length() / 3 * fontSize;
        }
        else{
            sz=s->record().field(fp).length() * fontSize;
        }
        view->setColumnWidth(fp,sz);
        fp++;
    }
    fieldsCount=s->record().count();
    while(fp<fieldsCount){		//hide not shown fields
        view->hideColumn(fp);
        fp++;
    }
    int baseX=parent->geometry().left();		//parent dialog position
    int baseY=parent->geometry().top();		//parent dialog position
    this->move(baseX+x,baseY+y);
    this->resize(w,view->rowHeight(0) * linesCount);
    view->resize(w,view->rowHeight(0) * linesCount);
    this->exec();
    return rt;
}
//
QStringList sckwd::getValue(QSqlQuery * s, int showCount , int x , int y)//show top left point at x,y ,dialog size automatically 
{
    if(s->size()<1){
        QMessageBox::warning(this,tr("注意"),tr("找不到指定的检索内容！"));
        rt.clear();
        return rt;
    }
    if(s->size()==1){
        s->first();
        int fp=0;		//field position
        fieldsCount=s->record().count();
        rt.clear();
        while(fp<fieldsCount){
            rt << s->value(fp).toString();
            fp++;
        }
        return rt;
    }
    model->setQuery(*s);
    view->setModel(model);
    int fontSize=view->font().pointSize();
    int dlgWidth=0;		//this dialog width
    int fp=0;		//field position
    while(fp<showCount){	//set shown fields width
        int sz;
        if(s->record().field(fp).type() ==QVariant::String){
            sz=s->record().field(fp).length() / 3 * fontSize;
        }
        else{
            sz=s->record().field(fp).length() * fontSize;
        }
        view->setColumnWidth(fp,sz);
        dlgWidth=dlgWidth+sz;
        fp++;
    }
    dlgWidth+=20;	//scrollbar width
    fieldsCount=s->record().count();
    while(fp<fieldsCount){		//hide not shown fields
        view->hideColumn(fp);
        fp++;
    }
    int baseX=parent->geometry().left();		//parent dialog position
    int baseY=parent->geometry().top();		//parent dialog position
    this->move(baseX+x,baseY+y);
    this->resize(dlgWidth,view->rowHeight(0) * 8);
    view->resize(dlgWidth,view->rowHeight(0) * 8);
    this->exec();
    return rt;
}
//
QStringList sckwd::getValue(QSqlQuery * s, QList<int>ti , int x , int y)//show top left point at x,y ,dialog size automatically
{
    int showCount=ti.size();
    if(s->size()<1){
        QMessageBox::warning(this,tr("注意"),tr("找不到指定的检索内容！"));
        rt.clear();
        return rt;
    }
    if(s->size()==1){
        s->first();
        int fp=0;		//field position
        fieldsCount=s->record().count();
        rt.clear();
        while(fp<fieldsCount){
            rt << s->value(fp).toString();
            fp++;
        }
        return rt;
    }
    model->setQuery(*s);
    view->setModel(model);
    int fontSize=view->font().pointSize();
    int dlgWidth=0;		//this dialog width
    int fp=0;		//field position
    while(fp<showCount){	//set shown fields width
        int sz=ti.value(fp) * fontSize;
        view->setColumnWidth(fp,sz);
        dlgWidth=dlgWidth+sz;
        fp++;
    }
    dlgWidth+=20;	//scrollbar width
    fieldsCount=s->record().count();
    while(fp<fieldsCount){		//hide not shown fields
        view->hideColumn(fp);
        fp++;
    }
    int baseX=parent->geometry().left();		//parent dialog position
    int baseY=parent->geometry().top();		//parent dialog position
    this->move(baseX+x,baseY+y);
    this->resize(dlgWidth,view->rowHeight(0) * 8);
    view->resize(dlgWidth,view->rowHeight(0) * 8);
    this->exec();
    return rt;
}
//
void sckwd::showEvent(QShowEvent*)
{
    view->setFocus();
}
//
